package com.assets.asset.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.assets.asset.domain.TReturn;
import com.assets.asset.service.ITAssetService;
import com.assets.asset.annotation.ReturnComIdAndStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.assets.common.annotation.Log;
import com.assets.common.core.controller.BaseController;
import com.assets.common.core.domain.AjaxResult;
import com.assets.common.enums.BusinessType;
import com.assets.asset.service.ITReturnService;
import com.assets.common.utils.poi.ExcelUtil;
import com.assets.common.core.page.TableDataInfo;

/**
 * 退库Controller
 *
 * @author bin
 * @date 2023-03-13
 */
@RestController
@RequestMapping("/asset/return")
public class TReturnController extends BaseController
{
    @Autowired
    private ITReturnService tReturnService;

    @Autowired
    private ITAssetService tAssetService;

    /**
     * 查询退库列表
     */
    @PreAuthorize("@ss.hasPermi('asset:return:list')")
    @GetMapping("/list")
    public TableDataInfo list(TReturn tReturn)
    {
        startPage();
        List<TReturn> list = tReturnService.selectTReturnList(tReturn);
        return getDataTable(list);
    }

    /**
     * 导出退库列表
     */
    @PreAuthorize("@ss.hasPermi('asset:return:export')")
    @Log(title = "退库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TReturn tReturn)
    {
        List<TReturn> list = tReturnService.selectTReturnList(tReturn);
        ExcelUtil<TReturn> util = new ExcelUtil<TReturn>(TReturn.class);
        util.exportExcel(response, list, "退库数据");
    }

    /**
     * 获取退库详细信息
     */
    @PreAuthorize("@ss.hasPermi('asset:return:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        AjaxResult ajax = AjaxResult.success(tReturnService.selectTReturnById(id));
        ajax.put("assetAdmins",tAssetService.selectAdminList());
        return ajax;
    }

    /**
     * 新增退库
     */
    @PreAuthorize("@ss.hasPermi('asset:return:add')")
    @Log(title = "退库", businessType = BusinessType.INSERT)
    @PostMapping
    @ReturnComIdAndStatus
    public AjaxResult add(@RequestBody TReturn tReturn)
    {
//        AjaxResult ajax = AjaxResult.success(tReturnService.insertTReturn(tReturn));
//        ajax.put("assetUsers",tAssetService.selectAllUserList());
        return success(tReturnService.insertTReturn(tReturn));
    }

    /**
     * 修改退库
     */
    @PreAuthorize("@ss.hasPermi('asset:return:edit')")
    @Log(title = "退库", businessType = BusinessType.UPDATE)
    @PutMapping
//    @ReturnComId
    @ReturnComIdAndStatus
    public AjaxResult edit(@RequestBody TReturn tReturn)
    {
        return toAjax(tReturnService.updateTReturn(tReturn));
    }

    /**
     * 删除退库
     */
    @PreAuthorize("@ss.hasPermi('asset:return:remove')")
    @Log(title = "退库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tReturnService.deleteTReturnByIds(ids));
    }
}
